package com.twu.biblioteca;

import com.twu.biblioteca.menu.*;
import com.twu.biblioteca.user.LibraryUserAuthenticator;
import com.twu.biblioteca.user.User;
import java.io.IOException;
import java.util.HashMap;

public class BibliotecaApp {

    private static final String GUEST_USERNAME = "guest";
    private static final MenuInputReader reader = new MenuInputReader();

    public static void main(String[] args) {
        User user = new User("Nat", "0416544552", "nat.young7@gmail.com");
        user = login(user);
        Index index = new Index();
        addFiveBooksToIndex(index);
        addFifteenMoviesToIndex(index);
        HashMap<Integer, MenuItem> menuItems = new HashMap<Integer, MenuItem>();
        MenuItem bookList = MenuItemFactory.createBookList(user, index);
        MenuItem movieList = MenuItemFactory.createMovieList(user, index);
        MenuItem membershipDetails = MenuItemFactory.createMembershipDetails(user);
        menuItems.put(1, bookList);
        menuItems.put(2, movieList);
        menuItems.put(3, membershipDetails);
        Menu menu = new Menu(menuItems, reader);
        run(menu);
    }

    private static User login(User user)
    {
        try
        {
            LibraryUserAuthenticator authenticator = createLibraryUserAuthenticator();
            System.out.println("Log in or enter '" + GUEST_USERNAME + "' as the username.");
            System.out.print("Username: ");
            String username = reader.readString();
            if(username.equals(GUEST_USERNAME))
            {
                return user;
            }
            System.out.print("Password: ");
            String password = reader.readString();
            user.setUserId(username);
            user.setPassword(password);
            user = authenticator.authenticateUser(user);
            printLogInStatus(user);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return user;
    }

    private static LibraryUserAuthenticator createLibraryUserAuthenticator()
    {
        String username = "111-1111";
        String password = "xyz";
        HashMap<String, String> logins = new HashMap<String, String>();
        logins.put(username, password);
        return new LibraryUserAuthenticator(logins);
    }

    private static void printLogInStatus(User user)
    {
        if(user.isLoggedIn())
        {
            System.out.println("Logged in as " + user.getName());
        }
        else
        {
            System.out.println("Log in failed. Entering as guest.");
        }
    }

    private static void addFifteenMoviesToIndex(Index index)
    {
        for(int i=0; i<15; i++)
        {
            int rating = i;
            if(rating > 10)
            {
                rating = 5;
            }
            String title = "Movie " + i;
            String director = "Director " + rating;
            Movie movie = new Movie(rating, 1990, title, director);
            index.add(movie);
        }
    }

    private static void addFiveBooksToIndex(Index index)
    {
        for(int i=0; i<5; i++)
        {
            index.add(new Book());
        }
    }

    private static void run(Menu menu)
    {
        try
        {
            menu.run();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
}
